package com.zzf.concurrent.example.exception;

import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;

/**
 * @author GaoFeng2017
 * @date 2019/8/8 0:25
 */

@Slf4j
public class ExceptionExample {
    public static void main(String[] args) {
        // 捕获一个线程的异常
        exceptionCapture();
        // 获取执行方法堆栈信息
    }

    private static void  exceptionCapture() {
        Thread t1 = new Thread(() -> {
            int a = 1 / 0;
        });

        t1.setUncaughtExceptionHandler((t, e) -> {
            log.info("{}, {}", t, e);
        });
        t1.start();
        StackTraceElement[] stackTrace = t1.getStackTrace();
        Arrays.asList(stackTrace)
                .stream()
                .filter(e -> !e.isNativeMethod())
                .forEach(e -> log.info(e.getClassName() + " " + e.getFileName() + " " +
                        e.getLineNumber() + " " + e.getMethodName()));

    }

}
